<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------
/**
 * Class Driver
 * Desc  司机Model
 * Create on 2024/10/26 17:45
 * Create by wangyafang
 */

namespace app\api\model\wddj\driver;


use EasyWeChat\Factory;
use think\Model;

class Driver extends Model
{
// 表名
    protected $name = 'wddj_driver';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'applet_driver_report_order_path'
    ];

    public static function getDriverBaseInfo($id)
    {

        $driverObj = self::where('d.id',$id)
            ->alias('d')
            ->join('wddj_certified_driving_agent certified','d.wechat_id = certified.wechat_id')
            ->field('certified.name,d.wechat_id,d.driver_lng,d.driver_lat')
            ->find();

        $acceptance_number = (new Order())->where('state','in',['accepted','in_progress','adjusting','refunded','awaiting_payment','completed'])
        ->where('driver_id',$id)
        ->where('driver_wechat_id',$driverObj->wechat_id)
        ->count();
        $userWechatObj = (new UserWechat())->get($driverObj->wechat_id);
        if ($userWechatObj['avatar'] == '/assets/addons/wddj/img/user.png') {
            $userWechatObj['avatar'] = '/assets/addons/wddj/img/driver.png';
        }
        $avatar = wddj_full_url($userWechatObj['avatar']);
        return array(
            'name'=>mb_substr($driverObj['name'], 0, 1, 'UTF-8').'师傅',
            'acceptance_number'=>$acceptance_number,
            'lng'=>$driverObj['driver_lng'],
            'lat'=>$driverObj['driver_lat'],
            'avatar'=>$avatar,
        );

    }

    public static function getMobile($id)
    {
        $mobile = self::where('d.id',$id)
            ->alias('d')
            ->join('wddj_user_wechat wechat','d.wechat_id = wechat.id')
            ->value('wechat.mobile');
        return $mobile;
    }

    /**
     * Desc 生成司机报单小程序二维码
     * Create on 2025/5/15 14:56
     * Create by wangyafang
     */
    public function getAppletDriverReportOrderPathAttr($value, $data)
    {
        $row = $data;
        $row['createtime'] = self::where('id',$row['id'])->value('createtime');
        $save_path = '/uploads/wddj/applet/driver/report_order/'.$row['id'].'/'.$row['createtime'].'.png';
        if (is_file(ROOT_PATH."public".$save_path)) {
            $value = $save_path;
        }  else {
            $ids = $row['id'];
            $configObj = (new \app\admin\model\wddj\Config())->where('id',1)->find();
            if (!empty($configObj['applet_appid']) && !empty($configObj['applet_secret'])) {
                $path = 'pages/passenger/index/order';
                $config = [
                    'app_id' => $configObj['applet_appid'],
                    'secret' => $configObj['applet_secret'],
                    'response_type' => 'array',
                    'log' => [
                        'level' => 'debug',
                    ],
                ];

                $app = Factory::miniProgram($config);

                $response  = $app->app_code->getUnlimit($ids, [
                    'page'  => $path,
                    'check_path'  => false,//小程序正式版本
                ]);

                if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
                    $response->saveAs('uploads/wddj/applet/driver/report_order/'.$row['id'], $row['createtime'].'.png');
                    $value = $save_path;
                }
            }
        }
        if (!empty($value)) {
            $value = request()->domain().$value;
        }
        return $value;
    }
}



 